﻿package ;

import php.Lib;
import php.Web;
import php.db.Manager;
import php.db.Object;

/**
 * ...
 * @author Alex
 */



class Users extends Object {
	
	public var ID:   Int;
	public var Name: String;
	public var Pass: String;
	public var Sex:  String;
	public var Age:  Int;
	public var city: String;
	
	public static var manager = new php.db.Manager<Users>(Users);

	public function new(Name: String, Pass: String, Sex: String, Age: Int, city: String) {
		super();
		this.Name = Name;
		this.Pass = Pass;
		this.Sex  = Sex;
		this.Age  = Age;
		this.city = city;
		
	}
}

class Main 
{
	static function main() 
	{
		var params = Web.getParams();
		
		try {
			var connection = php.db.Mysql.connect( {
				host : "127.0.0.1",
				port : 3306,
				user : "root",
				pass : "",
				socket : null,
				database : "users"
				});
			connection.request("SET NAMES 'utf8';");
			Manager.cnx = connection;
		} catch (e: Dynamic) {
			trace(e);
			return;
		}
		
		switch (params.get('act')) {
			
			case "add":
				var Name = params.get('Name');
				var Pass = params.get('Pass');
				var Sex  = params.get('Sex');
				var Age  = Std.parseInt(params.get('Age'));
				var city = params.get('city');
				
				add(Name, Pass, Sex, Age, city);
				
			case "delete": 
				var ID = Std.parseInt(params.get('ID'));
				delete(ID);
				
			case "list":
				list();
				
			default:			
		}
	}
		private static function add(Name: String, Pass: String, Sex: String, Age: Int, city: String) {
			var users = new Users(Name, Pass, Sex, Age, city);
				users.insert();
				Lib.print("gracefully");
	}
		private static function delete(ID: Int) {
			Users.manager.delete( { ID: ID } );
			Lib.print("lamentably");
	}
		private static function list() {
			var users = Users.manager.all();
				for (users in users) {
					Lib.print(users.ID + " " + users.Name + " " + users.Pass + users.Sex + " " + users.Age + " "+ users.city +"<br>");
		}
	}

}